package com.aplink.generic.util;

import java.util.Locale;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;

import com.aplink.generic.constant.MasterConfigContant;
import com.aplink.generic.controller.GenericApplication;

public final class ResourcesHelper {
    public static final int RESOURCE_NOT_FOUND = 0;

    public static int getAnimationIdByName(final String colorName) {
        return ResourcesHelper.getResourceByName(colorName, "anim");
    }

    public static int getColorIdByName(final String colorName) {
        return ResourcesHelper.getResourceByName(colorName, "color");
    }

    public static int getDrawableIdByName(final String drawableName) {
        return ResourcesHelper.getResourceByName(drawableName, "drawable");
    }

    public static int getLayoutIdByName(final String layoutName) {
        return ResourcesHelper.getResourceByName(layoutName, "layout");
    }

    public static int getMenuIdByName(final String menuName) {
        return ResourcesHelper.getResourceByName(menuName, "menu");
    }

    public static int getRawIdByName(final Context context, final String rawName) {
        return ResourcesHelper.getResourceByName(context, rawName, "raw");
    }

    public static int getRawIdByName(final String rawName) {
        return ResourcesHelper.getResourceByName(rawName, "raw");
    }

    private static int getResourceByName(final Context context,
            final String name, final String type) {
        if (TextUtils.isEmpty(name)) {
            LogUtil.info(LogUtil.RESOURCE_HELPER, String.format(
                    "Your resouce from %s is null or empty", type),
                    "getResourceByName");
        }
        if ((context == null) || (name == null) || name.isEmpty()) {
            LogUtil.info(LogUtil.RESOURCE_HELPER, name + " is not found",
                    "getResourceByName");
            return ResourcesHelper.RESOURCE_NOT_FOUND;
        }
        return context.getResources().getIdentifier(name, type,
                context.getPackageName());
    }

    private static int getResourceByName(final String name, final String type) {
        if (TextUtils.isEmpty(name)) {
            LogUtil.info(LogUtil.RESOURCE_HELPER, String.format(
                    "Your resouce from %s is null or empty", type),
                    "getResourceByName");
        }
        final Context context = GenericApplication.getInstance()
                .getApplicationContext();
        if ((context == null) || (name == null) || name.isEmpty()) {
            LogUtil.info(LogUtil.RESOURCE_HELPER, name + " is not found",
                    "getResourceByName");
            return ResourcesHelper.RESOURCE_NOT_FOUND;
        }
        return context.getResources().getIdentifier(name, type,
                context.getPackageName());
    }

    public static String getStringByName(final String stringName) {
        String title = stringName;
        if (!TextUtils.isEmpty(title)) {
            if (title.toLowerCase(Locale.getDefault()).contains(
                    MasterConfigContant.XML_STRING_FORMAT.toLowerCase(Locale
                            .getDefault()))) {
                final int titleId = ResourcesHelper.getResourceByName(title
                        .replace(MasterConfigContant.XML_STRING_FORMAT, ""),
                        "string");
                if (titleId != ResourcesHelper.RESOURCE_NOT_FOUND) {
                    final Resources resources = GenericApplication
                            .getInstance().getResources();
                    title = resources.getString(titleId);
                }
            }
        }
        return title;
    }

    public static int getViewIdByName(final String viewName) {
        return ResourcesHelper.getResourceByName(viewName, "id");
    }

    private ResourcesHelper() {

    }
}
